package com.hei.controller;

import com.hei.pojo.Barticle;
import com.hei.pojo.Bkind;
import com.hei.pojo.Blabel;
import com.hei.pojo.Buser;
import com.hei.service.BarticleService;
import com.hei.service.BkindService;
import com.hei.service.BlabelService;
import com.hei.util.FileUpload;
import com.hei.util.ResponseMessage;
import com.hei.util.uuid;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class EditController {
    @Autowired
    private BlabelService blabelService;

    @Autowired
    private BkindService bkindService;

    @Autowired
    private BarticleService barticleService;

    @RequestMapping("/toEdit")
    public String toEdit(Model model){
        Barticle barticle=new Barticle();
        barticle.setId("0");
        model.addAttribute("article",barticle);
        return "edit";
    }

    @RequestMapping("/getLabelKind")
    @ResponseBody
    public ResponseMessage getLabelKind(){
        Map<String,Object> map=new HashMap<>();
        map.put("publish","1");
        List<Bkind> kinds = bkindService.queryAllKind(map);
        List<Blabel> labels = blabelService.queryAllLabel(map);
        return ResponseMessage.success(200,"成功").addObject("kinds",kinds).addObject("labels",labels);
    }

    @PostMapping("/article")
    public String addArticle(Barticle article, @RequestParam("file")MultipartFile file, HttpServletRequest request, HttpSession session) throws IOException {
        String content = request.getParameter("content-editormd-markdown-doc");
        article.setContent(content);
        article.setId(uuid.getUUId());
        article.setCreateTime(new Date());
        article.setPublish(0);
        Buser user = (Buser) session.getAttribute("user");
        article.setUserId(user.getId());
        if(!file.isEmpty()){
            String IMAGE_PATH="D:\\tomcatImage";
            String path = FileUpload.fileUpload(file, IMAGE_PATH);
            article.setPicture(path);
        }else{
            article.setPicture("background.png");
        }
        barticleService.insertArticle(article);
        return "redirect:/manage/manage";
    }

    @PostMapping("/updateArticle")
    public String updateArticle(Barticle article,@RequestParam("file")MultipartFile file,HttpServletRequest request) throws IOException {
        String content = request.getParameter("content-editormd-markdown-doc");
        article.setContent(content);
        article.setCreateTime(new Date());
        if(!file.isEmpty()){
            String IMAGE_PATH=request.getSession().getServletContext().getRealPath("/")+"static\\image\\";
            String path = FileUpload.fileUpload(file, IMAGE_PATH);
            article.setPicture(path);
        }
        barticleService.updateArticleById(article);
        return "redirect:/manage/manage";
    }
}
